package com.ctg.payroll2.action.sysadmin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctg.payroll2.bean.po.PbCity;
import com.ctg.payroll2.service.ProvinceCityService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class CityAction extends ActionSupport{
	private Page result; 
	private List<PbCity> resultL;
	private List<PbCity> resultCity;

	public String queryCity() {
		String CITY_CODE = null;
		String CITY_NAME = null;
		String CITY_ID = null;
		Integer start = null;
		Integer limit = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		CITY_CODE =  request.getParameter("CITY_CODE"); 
		CITY_NAME = request.getParameter("CITY_NAME");
		CITY_ID = request.getParameter("city_id");
		start = Parse.toInteger(request.getParameter("start") , 0);
		limit = Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE);
		//System.out.println(code  + "\t" + queryKey);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		try{
			ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbCityService");
			HashMap<String, String> hm=new HashMap<String, String>();
			hm.put("city_code", CITY_CODE);
			hm.put("city_name", CITY_NAME);
			hm.put("city_id", CITY_ID);
			result = provinceCityService.getProvinceCity(hm , start, limit);
		}catch(Exception e){
		
//			System.out.println(e.getMessage());
		
		}	

		return SUCCESS;
	}
	
	public String queryCityDetails() {
		Integer city_id = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		city_id = Parse.toInteger(request.getParameter("city_id"));
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService =(ProvinceCityService) wac.getBean("pbCityService");
		resultL = provinceCityService.query(city_id, null); 
		return SUCCESS;		 
	}
	
   public String delCity(){
	    String city = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		city = request.getParameter("city_id");
		String[] array = splitStringToStringArray(city);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbCityService");
		List<PbCity> list = new ArrayList();
		
		PbCity o=null;
		try {
		for(int i=0;i<array.length;i++){
			 o=new PbCity();
			 o.setCity_id(Integer.parseInt(array[i]));
			 list.add(o);
		}
		
	 
		 provinceCityService.deleteProvinceCity(list);
			
		} catch (SQLException e) {
		 
			e.printStackTrace();
		}
	   return SUCCESS;
   }
	
	public String addCity() {
		String city = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		city = request.getParameter("city");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbCityService");
		provinceCityService.insertByJson(city);
		return SUCCESS;
	}
	
	
	public String updateCity() {
		String city = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		city = request.getParameter("city");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService cityCityService = (ProvinceCityService) wac.getBean("pbCityService");
		cityCityService.updateByJson(city);
		return SUCCESS;
	}
 
	
	public static String[] splitStringToStringArray(String args) {
		JSONArray json = JSONArray.fromObject(args);
		Object[] ss = json.toArray();
		String[] array = new String[ss.length];
		for (int i = 0; i < ss.length; i++) {
			//System.out.println("====="+ss[i]);
			String s =  ss[i]+"";
			array[i] = s;
		}
		return array;
	}
	
	/**
	 * 省市级联：通过省的id查询城市
	 * @param province_id
	 * @return
	 */
    @SuppressWarnings("unchecked")
	public String queryCityByProviceId(){
    	HttpServletRequest request=ServletActionContext.getRequest();
    	String province_id=request.getParameter("province_id");
    	WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		ProvinceCityService provinceCityService = (ProvinceCityService) wac.getBean("pbCityService");
		resultCity=provinceCityService.queryCityByProviceId(Integer.parseInt(province_id));
    	return SUCCESS;
    }
	
	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	public List<PbCity> getResultL() {
		return resultL; 
	}

	public void setResultL(List<PbCity> resultL) {
		this.resultL = resultL;
	}

	public List<PbCity> getResultCity() {
		return resultCity;
	}

	public void setResultCity(List<PbCity> resultCity) {
		this.resultCity = resultCity;
	}
}
